package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.SaleList;
import com.atguigu.jxc.entity.GoodVo;
import com.atguigu.jxc.entity.SaleDataVo;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.service.SaleListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/saleListGoods")
public class SaleListGoodsController {

    @Autowired
    private SaleListGoodsService saleListGoodsService;

    private static final ObjectMapper MAPPER = new ObjectMapper();


    @PostMapping("/count")
    @RequiresPermissions(value = "商品销售统计")
    public String count (String sTime, String eTime ,Integer goodsTypeId, String codeOrName) throws JsonProcessingException {
        List<GoodVo> goodVos = this.saleListGoodsService.count(sTime, eTime, goodsTypeId, codeOrName);
        return MAPPER.writeValueAsString(goodVos);
    }

    @PostMapping("/getSaleDataByDay")
    @RequiresPermissions(value = "按日统计分析")
    public String getSaleDataByDay(String sTime, String eTime) throws JsonProcessingException {
       List<SaleDataVo> dataVos = this.saleListGoodsService.getSaleDataByDay(sTime, eTime);
        return MAPPER.writeValueAsString(dataVos);
    }

    @PostMapping("/getSaleDataByMonth")
    @RequiresPermissions(value = "按月统计分析")
    public String getSaleDataByMonth(String sTime, String eTime) throws JsonProcessingException {
        List<SaleDataVo> dataVos = this.saleListGoodsService.getSaleDataByMonth(sTime, eTime);
        return MAPPER.writeValueAsString(dataVos);
    }



    /**
     * 支付结算（修改销售单付款状态）
     *
     * @param saleListId
     * @return
     */
    @PostMapping("updateState")
    public ServiceVO updateState(Integer saleListId) {
        this.saleListGoodsService.updateState(saleListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @PostMapping("/list")
    public Map<String, Object> list(String saleNumber, Integer customerId, Integer state, String sTime, String eTime) {
        return saleListGoodsService.list(saleNumber, customerId, state, sTime, eTime);
    }

    @PostMapping("/goodsList")
    public Map<String, Object> goodsList(Integer saleListId) {
        return saleListGoodsService.goodsList(saleListId);
    }

    @PostMapping("/delete")
    public ServiceVO delete(Integer saleListId) {
        return saleListGoodsService.delete(saleListId);
    }

    @PostMapping("/save")
    public ServiceVO save(SaleList saleList, String saleListGoodsStr) {
        return saleListGoodsService.save(saleList, saleListGoodsStr);
    }
}
